<?php
/**
 * User: hjd-yao
 * Date: 2018/11/6
 * Time: 16:53
 */

namespace App\Models\System;


use App\Models\BaseModel;
use App\Models\ManageAdminModel;

class RoleModel extends BaseModel
{
    protected $table = 'sys_roles';

    public $timestamps = false;

    public function addRole($data)
    {
        $role = new static();
        $role->title = $data['role_title'];
        $role->comment = $data['comment']??'';
        $role->my_auth = $data['my_auth']??'';
        $role->status = $data['status'];
        $role->mgr_id = ManageAdminModel::init()->getLoginId();
        $role->created_at = time();
        $role->created_ip = get_client_ip(1);
        return $role->save();
    }

    /**
     * @title 我的权限
     * @param $role_ids
     * @return bool
     */
    public function myAuth(array $role_ids)
    {
        $my_auth = self::whereIn('id',$role_ids)->pluck('my_auth')->toArray(); //查询角色拥有的权限
        if(!$my_auth){
            return false;
        }
        $my_auth_str = arr2str($my_auth);
        $my_actions = AuthModel::whereIn('id',str2arr($my_auth_str))->select('action','relate_action')->get()->toArray();
        if(!$my_actions){
            return false;
        }
        $main_actions = array_column($my_actions,'action');
        $relate_actions = array_filter(array_column($my_actions,'relate_action'));
        $my_actions = array_merge($main_actions,str2arr(arr2str($relate_actions)));
        return $my_actions;
    }

}